package com.tvunetworks.center.user.controller.feign;

import com.github.pagehelper.PageInfo;
import com.tvunetworks.center.common.constant.UserBehaviorConstant;
import com.tvunetworks.center.common.feign.UserFeignInterface;
import com.tvunetworks.center.common.model.*;
import com.tvunetworks.center.common.model.dto.UserWithPeerIds;
import com.tvunetworks.center.common.model.dto.user.DeviceBelongUserDTO;
import com.tvunetworks.center.common.model.dto.user.UserFeatureDTO;
import com.tvunetworks.center.common.model.param.ListUserPeerIdsParam;
import com.tvunetworks.center.common.model.param.UserDeviceRelationParam;
import com.tvunetworks.center.common.model.vo.ChannelUsingCount;
import com.tvunetworks.center.common.model.vo.UserInfoVo;
import com.tvunetworks.center.common.util.JsonUtil;
import com.tvunetworks.center.common.util.PasswordEncodeUtil;
import com.tvunetworks.center.common.util.Result;
import com.tvunetworks.center.common.util.ResultUtil;
import com.tvunetworks.center.gs.model.Device;
import com.tvunetworks.center.gs.model.DeviceExtend;
import com.tvunetworks.center.gs.model.DeviceGeo;
import com.tvunetworks.center.user.constant.UserConstant;
import com.tvunetworks.center.user.constant.UserDeviceRecordConstant;
import com.tvunetworks.center.user.model.BookmarkDevice;
import com.tvunetworks.center.user.model.Menu;
import com.tvunetworks.center.user.model.result.ResultConstant;
import com.tvunetworks.center.user.model.vo.BindDeviceUserVo;
import com.tvunetworks.center.user.model.vo.MenuListVo;
import com.tvunetworks.center.user.model.vo.UserBookmarkVo;
import com.tvunetworks.center.user.service.*;
import com.tvunetworks.center.user.service.feign.DeviceFeignService;
import com.tvunetworks.center.user.service.feign.UserAuthFeignService;
import com.tvunetworks.center.user.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: tvucc-user
 * @description: UserFeignController
 * @author lebronchen
 * @create: 2019-03-04 17:25
 **/
@RestController
@RequestMapping("/feign")
@Slf4j
public class UserFeignController implements UserFeignInterface {

    @Autowired
    private UserService userService;
    @Autowired
    private UserHttpService userHttpService;
    @Autowired
    private UserDeviceService userDeviceService;
    @Autowired
    private UserRoleService userRoleService;

    @Autowired
    private UserBehaviorService userBehaviorService;
    @Autowired
    private MenuService menuService;

    @Autowired
    private UserFeatureService userFeatureService;

    @Autowired
    private DeviceGeoService deviceGeoService;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private BookmarkDeviceService bookmarkDeviceService;

    @Autowired
    private UserBookmarkService userBookmarkService;
    @Autowired
    private DeviceFeignService deviceFeignService;
    @Autowired
    private ExtService extService;
    @Autowired
    private UserFeignService userFeignService;
    @Autowired
    private UserAuthFeignService userAuthFeignService;

    @Value("${superAdminAccount}")
    private String superAdminAccount;
    @Value("${superAdminPwd}")
    private String superAdminPwd;
    @Value("${rediskey.cache.superAdminSession}")
    private String superAdminSessionKey;
    @Value("${redisTTL.cache.superAdminSession}")
    private long superAdminSessionTTL;

    @Override
    public Result bindAndUnBindUserPackAndAnywhere(@RequestBody UserDeviceRelationParam param) {
        if (StringUtils.isEmpty(param.getServiceName())) {
            return ResultConstant.UserDevice.SERVER_NAME_IS_EMPTY;
        }
        String result = userDeviceService.bingUserPackAndAnywhere(param.getUserId(), param.getOperationUserId(),
                param.getBindPeerIds(), param.getUnBindPeerIds(), param.getServiceName());
        if (StringUtils.isNotEmpty(result)) {
            return ResultUtil.error("0x1", result);
        }
        return ResultUtil.success();
    }

    @Override
    public List<Device> listUserDevice(@RequestBody PageUserDeviceParam param) {
        return userDeviceService.listUserDevice(param);
    }

    @Override
    public Result<String> deleteExt(@RequestParam("peerId") String peerId) {
        return extService.deleteExt(peerId);
    }

    @Override
    public Result<String> getSessionsByEmail(String email) {
        return userService.getSessionByEmail(email);
    }

    @Value("${enable.addAndRemoveRPair:false}")
    private boolean enableAddAndRemoveRPair;
    @Value("${enable.addAndRemoveSourcePair:false}")
    private boolean enableAddAndRemoveSourcePair;

    @Override
    public LoginUser getCCUser(@RequestParam String email) {
        com.tvunetworks.center.user.model.User ccUser = userService.getByEmail(email);
        if (ccUser == null) {
            return null;
        }
        LoginUser loginUser = new LoginUser();
        BeanUtils.copyProperties(ccUser, loginUser);
        return loginUser;
    }

    @Override
    public LoginUser testPostBody(@RequestBody LoginUser loginUser) {
        return loginUser;
    }

    @Override
    public Result<LoginUser> getLoginUserAndCheckAuth(@RequestParam String token, @RequestParam String serverName) {
        try {
            return userService.getLoginUserAndCheckAuth(token, serverName);
        } catch (IOException e) {
            log.error(e.toString(), e);
            return ResultUtil.error("104", "Server error:" + e.toString() );
        }
    }

    @Override
    public Result<LoginUser> getLoginUser(@RequestParam String token) {
        try {
            return userService.getLoginUser(token);
        } catch (IOException e) {
            log.error(e.toString(), e);
            return ResultUtil.error("104", "Server error:" + e.toString() );
        }
    }

    @Override
    public Result<User> getUserInfo(@RequestParam String token) {
        try {
            return userService.getUserWithNoCheck(token);
        } catch (IOException e) {
            log.error("getUserInfo failed", e);
            return ResultUtil.error("104", "Server error:" + e.toString() );
        }
    }

    @Override
    public Result<LoginUser> getLoginUserWithCheck(@RequestParam String token) {
        try {
            return userService.getLoginUserWithCheck(token);
        } catch (IOException e) {
            log.error(e.toString(), e);
            return ResultUtil.error("104", "Server error:" + e.toString() );
        }
    }

    @Override
    public Result<LoginUser> getLoginUserWithNoCheck(@RequestParam String token) {
        try {
            return userService.getLoginUserWithNoCheck(token);
        } catch (IOException e) {
            log.error(e.toString(), e);
            return ResultUtil.error("104", "Server error:" + e.toString() );
        }
    }

    @Override
    public Result<Void> logout(@RequestParam String token) {
            return  userAuthFeignService.logOut(token);
         //   return userService.logout(token);

    }

    @Override
    public Result<String> getUserRole(@RequestParam String email, @RequestParam String token) {
        return userService.getUserRole(email, token);
    }

    @Override
    public Result<List<String>> getTpcFeatureEnable(@RequestBody GetTpcFeatureEnableParam param) {
        try {
            return userHttpService.getTpcFeatureEnable(param);
        } catch (IOException e) {
            log.error(e.toString(), e);
            return ResultUtil.error("103", "TpcFeatureEnable error");
        }
    }

    @Override
    public List<String> listUserRids(String userId) {
        return userDeviceService.listUserCacheRids(userId);
    }

    @Override
    public List<String> listUserTids(String userId) {
        return userDeviceService.listUserCacheTids(userId);
    }

    @Override
    public List<String> listUserXids(String userId) {
        return userDeviceService.listUserCacheXids(userId);
    }

    @Override
    public List<String> listUserDeviceIds(@RequestParam String userId) {
        return userDeviceService.listUserCacheDeviceIds(userId);
    }

    @Override
    public List<Device> listLiveRInfo(@RequestParam String userId,@RequestParam String condition) {

        return userDeviceService.listLiveRInfo(userId,condition);
    }

    @Override
    public boolean checkUserRole(@RequestParam String userId, @RequestParam String feature) {
        return userRoleService.checkUserRole(userId, feature);
    }

    @Override
    public List<UserFeatureDTO> listUserRole(@RequestParam String userId) {
        return userRoleService.listUserRole(userId);
    }


    @Override
    public List<DeviceExtend> listCurrentDeviceForTaskInfo(@RequestParam String userId, @RequestBody ListDeviceParam param) {

        return userDeviceService.listCurrentDeviceForTaskInfo(userId,param);
    }

    @Override
    public LoginUser getUserByEmail(@RequestParam String email) {
        return userService.getLoginUserByEmail(email);
    }

    @Override
    public Result<LoginUser> globalGetLoginUser(String token, boolean userServiceRole, String serviceName, boolean checkTvuccDB) {
        try {
            return userService.globalGetLoginUser(token, userServiceRole, serviceName, checkTvuccDB);
        } catch (IOException e) {
            log.error(e.toString(), e);
            return ResultUtil.error("104", "Server error:" + e.toString() );
        }
    }

    @Override
    public Result<String> getSuperAdminSession() {
        String session = (String)redisUtil.get(superAdminSessionKey);
        if (session != null) {
            return ResultUtil.success(session);
        }
        Result<String> result = this.getSession(superAdminAccount, superAdminPwd);
        if (result.assertSuccess() && result.getResult() != null) {
            redisUtil.set(superAdminSessionKey, result.getResult(), superAdminSessionTTL);
        }
        return result;
    }

    @Override
    public Result<String> getSession(@RequestParam String email, @RequestParam String password) {
        return userHttpService.getSession(email, PasswordEncodeUtil.encode(password));
    }

    @Override
    public Result<String> getSessionByEncodePassword(@RequestParam String email, @RequestParam String password) {
        return userHttpService.getSession(email, password);
    }

    @Override
    public Result<String> getGetGeoLocationByAddress(@RequestParam String param) {
        return userHttpService.getGetGeoLocationByAddress(param);
    }

    @Override
    public UserBehavior userBehavior(@RequestParam String userId, @RequestParam String pn) throws Exception {
        return userBehaviorService.userBehavior(userId,"dn");
    }

    @Override
    public void updataUserBehavior(@RequestBody UserBehavior userBehavior) throws Exception {
        userBehaviorService.updataUserBehavior(userBehavior);
    }
    @Override
    public void saveUserBehavior(@RequestBody UserBehavior userBehavior) throws Exception {
        userBehaviorService.updataUserBehavior(userBehavior);
    }
    @Override
    public UserBehavior queryUserBehavior(@RequestBody UserBehavior userBehavior) throws Exception {
        return userBehaviorService.queryUserBehavior(userBehavior);
    }

    @Override
    public List<Device> listUserSourceDevices(@RequestParam String userId, @RequestBody(required = false) SearchDeviceParam param) {
        return userDeviceService.listUserSourceDevices(userId,param);
    }

    @Override
    public List<Device> listReceiverControlR(@RequestParam String userId, @RequestBody(required = false) SearchDeviceParam param) {
        return userDeviceService.listReceiverControlR(userId,param);
    }

    @Override
    public LoginUser getLoginUserByUserId(@RequestParam String userId) throws Exception {
        return userService.getLoginUserByUserId(userId);
    }
    @Override
    public LoginUser getParentUserByUserId(@RequestParam String userId) throws Exception {
        return userService.getParentUserByUserId(userId);
    }

    @Override
    public Result<Object> listSourceR(@RequestParam String userId, @RequestBody(required = false) SearchSourcePairedRParam param)  {
        PageInfo<Device> deviceList = userDeviceService.pageSourceAvailableR(userId,param);
        return ResultUtil.success(deviceList);
    }
    @Override
    public Result<Object> listSource(@RequestParam String userId,@RequestBody(required = false) SearchDeviceParam param)  {
        PageInfo<Device> deviceList = userDeviceService.pageMySource(userId,param);
        return ResultUtil.success(deviceList);
    }
    @Override
    public int countGridNotifyNum(@RequestParam String userId) {
        return userService.countGridNotifyNum(userId);
    }
    @Override
    public Map<String,Object> checkUserPageMarkPermission(@RequestBody LoginUser user, @RequestParam String pageMark){
        Map<String,Object> permission = new HashMap();
        permission.put("pageMark",pageMark);
        try {
            Menu vo = menuService.getUserPageMarkPermission(user.getId(),user.getRole(),pageMark);
            if(vo ==null ){
                permission.put("permission",false);log.error("===checkUserPageMarkPermission null ====");
            }else {
                permission.put("permission", vo.isPermission());
            }
            if (vo!=null){
                if(!vo.isPermission()){//如果默认页面为false,则需要返回该用户下第一个有权限的页面
                    List<Menu> menus = new ArrayList<>();
                    MenuListVo menuInfoListVo = menuService.getMenuInfoList(user.getId(),user.getRole());
                    menus.addAll(menuInfoListVo.getNormalList());
                    if (!CollectionUtils.isEmpty(menuInfoListVo.getMoreList())) {
                        menus.addAll(menuInfoListVo.getMoreList());
                    }
                    for (Menu menu:menus) {
                        if(menu.isPermission()){
                            permission.put("defeaultPage",menu.getDataMark());
                        }
                    }
                }else {
                    permission.put("defeaultPage",pageMark);
                }
            }

        } catch (Exception e) {
            log.error("===checkUserPageMarkPermission exception====",e);
            permission.put("permission",false);
            permission.put("defeaultPage","st");
        }
        return permission;
    }

    @Override
    public Result<String> checkCopy(@RequestParam String oldPeerId, @RequestParam String newPeerId, @RequestParam String taskId) {
        return userDeviceService.checkCopy(oldPeerId, newPeerId, taskId);
    }

    @Override
    public Result<String> copyDevice(@RequestParam String oldPeerId, @RequestParam String newPeerId, @RequestParam String taskId) {
        return userDeviceService.copyDevice(oldPeerId, newPeerId, taskId);
    }

    @Override
    public Result<String> deleteDevice(@RequestParam String peerId, @RequestParam String taskId) {
        return userDeviceService.deleteDevice(peerId, taskId);
    }

    @Override
    public boolean userHasFeature(@RequestParam("userId") String userId,@RequestParam("featureName") String featureName) {
        return userFeatureService.userHasFeature(userId, featureName);
    }

    @Override
    public boolean deleteUser(@RequestParam("userId") String userId) {
        int result = userService.deleteUser(userId);
        log.error("delete user userId:{} success:{}", userId, result == 0);
        if (result == 1) {
            return false;
        }
        return true;
    }

    @Override
    public Map<String, Integer> batchGetGridTokenStatus(@RequestBody @Valid UserWithPeerIds param) {
        return userDeviceService.batchGetGridTokenStatus(param.getUserId(), param.getPeerIds());
    }

    @Override
    public DeviceBelongUserDTO listDeviceBelongUserForAlertDeviceChange(@RequestParam("peerId") String peerId) {
        return userDeviceService.listDeviceBelongUserForAlertDeviceChange(peerId);
    }

    @Override
    public Result<Object> getUserListByParentUserId(@RequestParam String parentUserId,@RequestParam String search,@RequestParam Integer currentPageNum,@RequestParam Integer everyPageNum) throws Exception {
        return userService.getUserListByParentUserId(parentUserId,search,currentPageNum,everyPageNum);
    }

    @Override
    public ChannelUsingCount channelCount(@RequestParam("userId") String userId, @RequestParam("manual") boolean manual) {
        return userDeviceService.channelCount(userId, manual);
    }

    @Override
    public List<DeviceGeo> getDeviceGeoByPeerIds(@RequestParam("peerIds") List<String> peerIds) {
        return deviceGeoService.getDeviceGeoByPeerIds(peerIds);
    }

    @Override
    public List<String> listUserPeerIds(@RequestBody ListUserPeerIdsParam param) {
        return userDeviceService.listUserPeerIds(param);
    }

    /**
     *
     * @param userId userId
     * @param peerId peerId
     * @return Result
     */
    public Result<Object> listUserDeviceBookmarksByPeerId(@RequestParam("userId") String userId,@RequestParam("peerId") String peerId){
        List<BookmarkDevice>  bookmarkDeviceList = bookmarkDeviceService.listUserDeviceBookmarksByPeerId(userId,peerId);
        return ResultUtil.success(bookmarkDeviceList);
    }

    /**
     *
     * @param userId userId
     * @param bookmarkId bookmarkId
     * @return Result
     */
    public Result<Object>  getAllParentBookmarkTree(@RequestParam("userId")String userId,@RequestParam("bookmarkId") String bookmarkId){
        List<UserBookmarkVo> userBookmarkVoList = userBookmarkService.getAllParentBookmarkTree(userId,bookmarkId);
        return ResultUtil.success(userBookmarkVoList);
    }
    @Override
    public List<String> getTokenAnywhereIds(@RequestParam("userId")String userId, @RequestParam(value = "rid",required = false) String rid,@RequestParam("tokenRegardlessOfUser")  boolean tokenRegardlessOfUser, @RequestParam(value = "condition",required = false) String condition){
        return userDeviceService.getTokenAnywhereIds(userId, rid, tokenRegardlessOfUser, condition, Boolean.TRUE);
    }

    @Override
    public List<String> listUserUnClassifiedPeerIds(@RequestParam("userId")String userId,@RequestParam(value = "condition",required = false) String condition,@RequestParam(value = "typeList",required = false)List<String> typeList) {
        return bookmarkDeviceService.listUserUnClassifiedPeerIds(userId,condition,typeList);
    }

    @Override
    public List<String> listUserPeerIdFilterByBookmarkAndCondition(@RequestParam("userId") String userId, @RequestBody SearchDeviceParam param) {
        return bookmarkDeviceService.listUserPeerIdFilterByBookmarkAndCondition(userId,param);
    }

    @Override
    public Result<String> getGeoNameByLocation(@RequestParam("location") String location) {
        return userHttpService.getGeoNameByLocation(location);
    }

    /**
     *
     * @param userId userId
     * @param email email
     * @param peerId peerId
     * @return Result
     */
    @PostMapping("/removeAllDevivecBindUser")
    public Result<Object> removeAllDevivecBindUser(@RequestParam String userId,@RequestParam String email, @RequestParam String peerId){
        if (StringUtils.isEmpty(peerId)){
            return ResultUtil.paramError();
        }
        List<String> idList = new ArrayList<>();
        idList.add(peerId);
        //把grid也同时删除
        idList.add(peerId+"0000000000000001");
        idList.add(peerId+"0000000000000002");
        try {
            for (String id:idList){
                List<BindDeviceUserVo> list = userDeviceService.listDeviceBindUser(id);
                StringBuffer userIdList = new StringBuffer();
                if(list !=null && list.size()>0){
                    for (int i = 0; i < list.size(); i++) {
                        if(i==0){
                            userIdList.append(list.get(i).getUserId());
                        }else {
                            userIdList.append(","+list.get(i).getUserId());
                        }
                    }
                    userDeviceService.removeDevivecBindUser(userIdList.toString(),id);
                    //记录操作日志
                    Map paramMap =  new HashMap();
                    paramMap.put("peerIdList",id);
                    paramMap.put("userIdList",userIdList);
                    userDeviceService.saveUserDeviceRecord(email,userId, UserDeviceRecordConstant.DEVICEUNBINDALLUSER,paramMap);
                }
                if(id.length()>16){//删除grid
                    deviceFeignService.deleteGrid(id);
                }

            }
            userDeviceService.deleteGrid(peerId);

        } catch (Exception e) {
            log.error("==removeAllDevivecBindUser exception=="+e.getMessage());
            return ResultUtil.error("81100601","remove all fail");//
        }
        return ResultUtil.success();
    }

    /**
     * Support 列出和设备有绑定关系的用户
     * @param peerId peerId
     * @return Result
     */
    @GetMapping("/listDeviceBindUser")
    public Result<Object> listDeviceBindUser(@RequestParam String peerId){
        List<BindDeviceUserVo> list = userDeviceService.listDeviceBindUser(peerId);
        return ResultUtil.success(list);
    }

    @Override
    public Result<String> addExtNoUser(@RequestParam("name") String name,@RequestParam("url") String url,@RequestParam("type") String type) {
        String remark = null;
        if(UserConstant.PlayOut.VIDEO.equals(type) ){
            remark = UserConstant.PlayOut.VIDEO;
        }
        if(UserConstant.PlayOut.FILE.equals(type) ){
            remark = UserConstant.PlayOut.FILE;
        }
        type="Ext";
        return extService.addExtNoUser(name, url, type, null, remark);
    }


    @Override
    public Result<String> addExtSource(@RequestParam("userEmail") String userEmail,@RequestParam("name") String name,@RequestParam("url") String url,@RequestParam("type") String type) {
        LoginUser loginUser = getCCUser(userEmail);
        String remark = null;
        if(UserConstant.PlayOut.VIDEO.equals(type) ){
            remark = UserConstant.PlayOut.VIDEO;
            type="Ext";
        }
        if(UserConstant.PlayOut.FILE.equals(type) ){
            remark = UserConstant.PlayOut.FILE;
            type="Ext";
        }
        Result<String> result = extService.addExtToUser(loginUser, name, url, type, null, remark);
        if ("81000101".equals(result.getErrorCode())) {
            result = extService.addExtToUser(loginUser, System.currentTimeMillis() + name, url, type, null, remark);
        }
        return result;
    }

    @Override
    public Result<String> createExtAndAddToUser(@RequestParam("userId") String userId,@RequestParam("name") String name,@RequestParam("url") String url,@RequestParam("type") String type) {
        com.tvunetworks.center.user.model.User user = userService.getById(userId);
        if (user == null) {
            return ResultUtil.error("101", "User not exist");
        }
        LoginUser loginUser = JsonUtil.convertValue(user, LoginUser.class);

        Result<String> result = extService.addExtToUser(loginUser, name, url, type, null, null);
        if ("81000101".equals(result.getErrorCode())) {
            result = extService.addExtToUser(loginUser, System.currentTimeMillis() + name, url, type, null, null);
        }
        return result;
    }
    @Override
    public Result listPreference(@RequestParam("userId") String userId, @RequestParam(value = "pn",required = false) String pn,
                                 @RequestParam(value = "content",required = false) String content){
        UserBehavior userBehavior = new UserBehavior();
        userBehavior.setUserId(userId);
        userBehavior.setRemark(UserBehaviorConstant.Remark.PREFERENCE);
        if(!StringUtils.isEmpty(pn)){
            userBehavior.setPn(pn);
        }
        if(!StringUtils.isEmpty(content)){
            userBehavior.setContent(content);
        }
        List<UserBehavior> behaviorList = userBehaviorService.userBehaviorList(userBehavior);
        return ResultUtil.success(behaviorList);
    }

    @Override
    public Result<Map<String,String>> getUserBehaviors(@RequestBody UserBehavior userBehavior){
        return userBehaviorService.getUserBehaviors(userBehavior);
    }

    @Override
    public Result<Object> getAllChildBookmarkTree(@RequestParam("userId") String userId){
        List<UserBookmarkVo> list = userBookmarkService.getAllChildBookmarkTree(userId);
        return ResultUtil.success(list);
    }

    @Override
    public Result<PageInfo<Device>> pageUserDevice(@RequestBody PageUserDeviceParam param) {
        return ResultUtil.success(userFeignService.pageUserDevice(param));
    }

    @Override
    public Result<Integer> getRoleByEmail(@RequestParam("email") String email) {
        return userFeignService.getRoleByEmail(email);
    }

    @Override
    public Result<List<UserInfoVo>> getSonUserByEmail(@RequestParam("email") String email) {
         return userService.sonUserByParentUserEmail(email);

    }

    @Override
    public Result updateDeviceExt(@RequestParam("peerId") String peerId, @RequestParam("name") String name,
                            @RequestParam("url") String url, @RequestParam("remark") String remark) {
        return extService.updateExt(peerId, name, url, remark);
    }

    @Override
    public boolean checkSourceIsOwnerByUserId(@RequestParam("userId") String userId, @RequestParam("peerId") String peerId) {
        return userDeviceService.checkSourceIsOwnerByUserId(userId, peerId);
    }

    @Override
    public void createUserInCcService(@RequestParam("userId") String userId, @RequestParam("email") String email) {
        userService.createUserInCcService(userId, email);
    }

    @Override
    public List<String> listExtByUserId(@RequestParam("userId") String userId, @RequestParam("condition") String condition) {
        return userDeviceService.listExtByUserId(userId, condition);
    }

    @Override
    public List<String> listOwnerGridId(@RequestParam("userId") String userId, @RequestParam("condition") String condition) {
        return userDeviceService.listOwnerGridId(userId, condition);
    }
    @Override
    public List<String> listOwnerXAndPackId(@RequestParam("userId") String userId, @RequestParam("condition") String condition) {
        return userDeviceService.listOwnerXAndPackId(userId, condition);
    }

    @Override
    public List<Device> listDeviceOfUser(@RequestParam("userId") String userId, @RequestBody SearchDeviceParam param) {
        return userDeviceService.listDeviceOfUser(userId, param);
    }



    /**
     * 添加用户和R绑定关系,包括R下的X和T
     * @param userId 用户id
     * @param rid rid
     * @return 是否成功
     */
    @Override
    public Result bingUserAndReceiver(String userId, String rid) {
        return userDeviceService.bingUserAndReceiver(userId, rid );
    }

    /**
     * 删除用户和R绑定关系,包括R下的X和T
     * @param userId 用户id
     * @param rid rid
     * @return 是否成功
     */
    @Override
    public Result deleteUserAndReceiver(String userId, String rid) {
        return userDeviceService.deleteUserAndReceiver(userId, rid );
    }

    /**
     * 根据父账号id获取宣布子账号id
     * @param userId userId
     * @return 子账号集合
     */
    @Override
    public List<String> listSonUserIdByParentId(@RequestParam("userId") String userId) {
        return userService.listSonUserIdByParentId(userId);
    }


    /**
     * 给用户授权Grid,如果用户已经有权限,则返回已拥有,没权限则授权
     * @param userId userId
     * @param gridId gridId
     * @return 是否成功
     */
    @Override
    public Result<String> setGridByUserId(@RequestParam("userId")String userId, @RequestParam("sourceId")String gridId) {
        return userDeviceService.setGridByUserId(userId, gridId );
    }


    @Override
    public List<String> listOwnerPackId(@RequestParam("userId") String userId, @RequestParam("condition") String condition) {
        return userDeviceService.listOwnerPackId(userId, condition );
    }

    @Override
    public List<String> pageOwnerAnywhere(String userId, String condition, String rid) {
        return userDeviceService.pageOwnerAnywhere(userId, condition ,rid);
    }
}
